var addPage;
avalon.validators.othersIndustry = {
	message: 'The other industry is required and must be less than 50 characters long!',
	get: function(value, field, next) {
		var companyType = $("#companyType").val();
		if((0 == companyType) && (companyType != "")) {
			if(value == '') {
				next(false);
			} else {
				var flag = value.length >= 50;
				next(!flag);
			}
		} else {
			next(true);
		}
		return value
	}
};
avalon.validators.postcodeV={
	message:'The postcode is required!',
	get:function(value, field, next){
		var country = $("#country option:selected").text();
		var _arr=['United Kingdom', 'Ireland'];
		if(_arr.indexOf(country)!=-1) {
			if(value =='') {
				next(false);
			}else {
				var flag = value.length >= 50;
				next(!flag);
			}
		}else {
			next(true);
		}
		return value
	}
};
avalon.validators.pwdmatch={
	get:function(value, field, next){
		var pwd = $("#password").val();
		if(value!=pwd) {
			next(false);
		}else {
			next(true);
		}
		return value
	}
};
// avalon.validators.cityRequired={
// 	get:function(value, field, next){
// 		var _required = $('#city_sup').is(':hidden');
// 		console.log(_required);
// 		if(!_required) {
// 			if(value =='') {
// 				next(false);
// 			}else {
// 				next(true);
// 			}
// 		}else {
// 			next(true);
// 		}
// 		return value
// 	}
// };
addPage = avalon.define({
	$id: "addPage",
	uid: "",
	companyName: "",
	inShort: "",
	taxNo: "",
	companyType: "",
	othersIndustry: "",
	email: "",
	passWord: "",
	passWordTwo: "",
	pwConfirmation: "",
	region: "",
	country: "",
	detailAddress: "",
	postcode:"",
	firstName:"",
	lastName:"",
	contactPerson: "",
	telNo: "",
	state: "",
	city: "",
	contact: "",
	jobTitle: "",
	existingDistributor: "",
	preferredDistributor: "",
	companySize: "",
	companyWebsite: "",
	aapisr: "",
	remark: "",
	statements: "",
	cityRequired:true,
	codes: window.DAHUA_CONFIG.CODES, //国码
	chooseCompanyType: function() {
		var companyType = this.companyType;
		if(0 == companyType) {
			this.othersIndustry = "";
			$("#i_others").removeClass("mui-hidden");
		} else {
			$("#i_others").addClass("mui-hidden");
		}
	},
	validate: {
		onValidateAll: function(reasons) {
			if(reasons.length) {
				// $(reasons[0].element).focus()
				mui.toast(reasons[0].getMessage());
			} else {
				var form = $("#addForm");
				var email=$(form).find('#userName').val();
				mui.ajax('/customer/reg.do', {
					data: form.serialize(),
					dataType: 'json',
					type: 'post',
					success: function(data) {
						if(1==data.status){
							mui.alert(data.msg, ' ', function() {
								// var url = "dahua://registerBack";
								var url = "dahua://login?email="+email;
								mui.openWindow({
									url: url 
								});
							});
						}else{
							mui.alert(data.msg, ' ', function() {});
						}						
					}
				});
			}
		}
	},
	add: function(e) {
		e.preventDefault()
		this.validate.onManual();
	}
});
mui.ready(function() {
	$("#locations").cxSelect({
		selects: ["region", "country", "state", "city"],
		required:false,
		firstTitle:$.i18n.prop("Please_choose")
	});
	mui.ajax('/registry.do', {
		data: {
			'name':'CUSTOMER_CITY_REQUIRED'
		},
		dataType: 'json',
		type: 'post',
		success: function(data) {
			if(null!=data&&data.CUSTOMER_CITY_REQUIRED){
				if(data.CUSTOMER_CITY_REQUIRED=='1') {
					addPage.cityRequired=true;
				}else {
					addPage.cityRequired=false;
				}
			}
			initCityRequired();		
		}
	});
	mui('.mui-input-row').on('change', '#country',function(){
		var _val=$(this).find('option:selected').text(),
			_arr=['United Kingdom', 'Ireland'];
		if(_arr.indexOf(_val)!=-1) {
			$('#postcodeDiv').show();
		}else {
			$('#postcodeDiv').hide();
		}
	});
	mui(".header").on('tap', '.login', function() {
		var url = "dahua://nativeback";
		mui.openWindow({
			url: url
		});
	});

	mui(".header").on('tap', '.home', function() {
		var url = this.getAttribute('href');
		mui.openWindow({
			url: url
		});

	});
//    mui(".mui-scroll").on('tap', 'a', function() {
//         var url = this.getAttribute('href');
//                          console.log(mui.plus);
//         var w = mui.plus.webview.create(url);
//         w.show(); // 显示窗口
//
////         mui.openWindow({
////             url: url
////          });ßß
//    });

//    mui(".mui-content").on('tap', '#cookieStatementShow', function() {
//         location.href="#cookieStatement";
//         });
    /*mui(".mui-row").on('tap', '#showStatement', function() {
        $("#regPage").addClass("mui-hidden");
        $("#statementPage").removeClass("mui-hidden");
    });*/
  var CX;
	mui(".mui-row").on('tap', '#showPrivacy', function() {
		$("#regPage").addClass("mui-hidden");
		$("#privacyText").removeClass("mui-hidden");
       CX = $('#cookieStatement').offset().top;
	});
	
	mui(".mui-row").on('tap', '#showTerms', function() {
		$("#regPage").addClass("mui-hidden");
		$("#termsText").removeClass("mui-hidden");
	});
	
	mui(".header").on('tap', '#backReg', function() {
		$("#statementPage").addClass("mui-hidden");
		$("#privacyText").addClass("mui-hidden");
		$("#termsText").addClass("mui-hidden");
		$("#regPage").removeClass("mui-hidden");
	});

    mui(".mui-row").on('tap', '#windowslocatinHtml', function() {
        var url = this.getAttribute('href');
         location.href = "dahua://openweb?" + url;
    });
          
	mui(".mui-row").on('tap', '#cookieStatementShow', function() {
	     $('#cookieStatementShow').offset().top;
	     mui("#privacyText_scroll").scroll().scrollTo(0,-CX+40);
	});
	function initCityRequired() {
		if(addPage.cityRequired) {
			$('#city').on('cxselect-init', function(e, r, d){
				if(r) {
					$('#city_sup').show();
					$(this).attr('ms-rules', '{required: true}');
					// addPage.cityRequired=true;
				}else {
					$('#city_sup').hide();
					$(this).attr('ms-rules', '{required: false}');
					// addPage.cityRequired=false;
				}
			});
		}else {
			$('#city_sup').hide();
		}
	}
});
